﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Security;
using System.Globalization;
using System.Threading;


public partial class masterpage_SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (!IsPostBack)
            {

                // BindCity();

                //BindCityForSubscribre();
                string PageName = GetCurrentPageName();
                if (PageName == "Default.aspx" || PageName == "Login.aspx")
                {
                    tdSub.Visible = true;
                    tdRefer.Visible = true;
                }
                else
                {
                    tdSub.Visible = false ;
                    tdRefer.Visible = false;
                }
                if ((Request.Cookies["CityID"] == null))
                {
                    MPECity.Show();
                }
                else
                {
                    MPECity.Hide();
                }

                if (Request.Cookies["CityName"] != null)
                {
                    lblCity.Text = Request.Cookies["CityName"].Value;
                    DateTime date = DateTime.Now.Date;
                    // lblDate.Text = date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.Year.ToString();
                }
                if (Membership.GetUser().UserName.ToString() != null)
                {
                    string UserID = Membership.GetUser().ProviderUserKey.ToString();
                    var FirstName = new DealesDataContext();
                    var GetFirstName = FirstName.Select_aspnet_Membership(Convert.ToString(UserID));
                    foreach (var item in GetFirstName)
                    {
                        LoginName1.FormatString = "<b>Hi " + item.FirstName.ToString() + ",</b>";

                    }

                }
            }


        }
        catch (Exception ex) { }
    }
    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        BindCityForSubscribre();
    }

    protected void RadListView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string DealID = (RadListView2.SelectedItems[0].GetDataKeyValue("DealID").ToString());
        Session["SideDeal"] = 1;
        Response.Redirect("~/Default.aspx?DealID=" + DealID);
    }

    protected void RadListViewMainDeal_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["SideDeal"] = null;
        string DealID = (RadListViewMainDeal.SelectedItems[0].GetDataKeyValue("DealID").ToString());
        Response.Redirect("~/Default.aspx?DealID=" + DealID);

    }

    public void BindCityForSubscribre()
    {
        dropCitySubscribe.Items.Clear();
        var deal = new DealesDataContext();
        var query = deal.City_ReturnForSubscribe();
        dropCitySubscribe.DataTextField = "City";
        dropCitySubscribe.DataValueField = "CityID";
        dropCitySubscribe.DataSource = query;
        dropCitySubscribe.DataBind();
        dropCitySubscribe.Items.Insert(0, new ListItem("-Select City-", "0"));
        dropCitySubscribe.SelectedIndex = 0;

        if (dropCitySubscribe.Items.Count <= 2)
        {
            tdSubscribe.Visible = false;
        }
        else
        {
            tdSubscribe.Visible = true;
        }
    }
    protected string TrimDescription(string description)
    {
        if (!string.IsNullOrEmpty(description) && description.Length > 60)
        {
            return string.Concat(description.Substring(0, 60), "...");
        }
        return description;
    }
    //public void BindCity()
    //{
    //    var deal = new DealesDataContext();
    //    var query = deal.City_ReturnForSubscribe();
    //    DLCity.DataSource = query;
    //    DLCity.DataBind();
    //    var deal2 = new DealesDataContext();
    //    var query2 = deal.City_ReturnForSubscribe();
    //    int CountCity = 0;
    //    foreach (var item in query2)
    //    {
    //        CountCity++;
    //    }
    //    if (CountCity <= 1)
    //    {
    //        city.Visible = false;  
    //    }
    //}
    //protected void DLCity_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    Response.Cookies.Clear();
    //    Response.Cookies["CityID"].Expires = DateTime.Now.AddYears(-30);
    //    Response.Cookies["CityName"].Expires = DateTime.Now.AddYears(-30);
    //    Response.Cookies["CityID"].Value = DLCity.DataKeys[DLCity.SelectedItem.ItemIndex].ToString();
    //    Response.Cookies["CityID"].Expires = DateTime.Now.AddDays(30);
    //    Response.Cookies["CityName"].Value = ((LinkButton)DLCity.Items[DLCity.SelectedIndex].FindControl("LinkButton1")).Text;
    //    Response.Cookies["CityName"].Expires = DateTime.Now.AddDays(30);
    //    string DealID = GetDealID(DLCity.DataKeys[DLCity.SelectedItem.ItemIndex].ToString());
    //    Response.Redirect("~/Default.aspx?DealID=" + DealID);

    //}
    protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
    {

        Session["username"] = null;





    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string CityID;
        string DealID = "0";
        if (Request.Cookies["CityID"] != null)
        {
            CityID = Request.Cookies["CityID"].Value;
            var sub = new DealesDataContext();
            var getDealID = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
            foreach (var item in getDealID)
            {
                DealID = item.DealID.ToString();

            }
        }

        Response.Redirect("~/Default.aspx?DealID=" + DealID);


    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        string CityID;
        string DealID = "0";
        if (Request.Cookies["CityID"] != null)
        {
            CityID = Request.Cookies["CityID"].Value;
            var sub = new DealesDataContext();
            var getDealID = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
            foreach (var item in getDealID)
            {
                DealID = item.DealID.ToString();

            }
        }

        Response.Redirect("~/Default.aspx?DealID=" + DealID);


    }
    public string GetDealID(string CityID)
    {

        string DealID = "0";
        var sub = new DealesDataContext();
        var getDealID = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
        foreach (var item in getDealID)
        {
            DealID = item.DealID.ToString();

        }

        return DealID;
    }
    protected void LinkButton2_click(object sender, EventArgs e)
    {

        Response.Redirect("~/MyAccountSettings.aspx");
        // Response.Redirect(Session["HttpsPath"].ToString() + "/MyAccountSettings.aspx");
    }
    protected void btnHome_Click(object sender, EventArgs e)
    {

        string CityID;
        string DealID = "0";
        if (Request.Cookies["CityID"] != null)
        {
            CityID = Request.Cookies["CityID"].Value;
            var sub = new DealesDataContext();
            var getDealID = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
            foreach (var item in getDealID)
            {
                DealID = item.DealID.ToString();

            }
        }

        Response.Redirect("~/Default.aspx?DealID=" + DealID);

    }
    protected void btnHowWeWork_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/HowWeWork.aspx");
    }
    protected void Company_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AboutUs.aspx");
    }
    protected void LearnMore_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/LearnMore.aspx");
    }
    protected void BusinessOwner_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/BusinessOwner.aspx");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Discussion.aspx");

    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/RecentDeal.aspx");

    }
    protected void linkrefferFriend_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ReferFriend.aspx");
    }
    protected void FaceBook_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/FaceBook.aspx");
    }
    protected void Twitter_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Twitter.aspx");
    }
    protected void AboutUs_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AboutUs.aspx");
    }
    protected void ContactUs_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ContactUs.aspx");
    }
    protected void FAQs_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/FAQs.aspx");
    }
    protected void TermAndCondition_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/TermAndCondition.aspx");
    }
    protected void PrivacyPolicy_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/PrivacyPolicy.aspx");
    }
    protected void Giftcards_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Giftcards.aspx");
    }

    protected void btnDailyEmail_Click(object sender, EventArgs e)
    {
        string CityID = "0";
        string CityName = "";
        if (Request.Cookies["CityID"] != null)
        {
            CityID = Request.Cookies["CityID"].Value;
            CityName = Request.Cookies["CityName"].Value;
        }

        Session["SubscribeDaialyDeal"] = "1";
        var sub = new DealesDataContext();
        DateTime UnSubScribeDate = Convert.ToDateTime("1/1/2020");
        if (tdSubscribe.Visible)
        {
            var query = sub.Insert_Subscribe(Convert.ToInt64(dropCitySubscribe.SelectedValue), txtdailyEmails.Text, true, Convert.ToDateTime(System.DateTime.Now), Convert.ToDateTime(UnSubScribeDate));
            Response.Cookies.Clear();
            Response.Cookies["CityID"].Value = Convert.ToString(dropCitySubscribe.SelectedValue);
            Response.Cookies["CityID"].Expires = DateTime.Now.AddDays(30);
            Response.Cookies["CityName"].Value = dropCitySubscribe.SelectedItem.Text.ToString();
            Response.Cookies["CityName"].Expires = DateTime.Now.AddDays(30);

            var query1 = sub.Select_DealByCityIDNew(Convert.ToInt32(dropCitySubscribe.SelectedValue));
            int DealID = 0;
            foreach (var item in query1)
            {
                DealID = Convert.ToInt32(item.DealID);
            }
            Response.Redirect("~/Default.aspx?DealID=" + DealID);
        }
        else
        {
            var query = sub.Insert_Subscribe(Convert.ToInt64(CityID), txtdailyEmails.Text, true, Convert.ToDateTime(System.DateTime.Now), Convert.ToDateTime(UnSubScribeDate));
            Response.Cookies.Clear();
            Response.Cookies["CityID"].Value = Convert.ToString(CityID);
            Response.Cookies["CityID"].Expires = DateTime.Now.AddDays(30);
            Response.Cookies["CityName"].Value = CityName;
            Response.Cookies["CityName"].Expires = DateTime.Now.AddDays(30);

            var query1 = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
            int DealID = 0;
            foreach (var item in query1)
            {
                DealID = Convert.ToInt32(item.DealID);
            }
            Response.Redirect("~/Default.aspx?DealID=" + DealID);
        }

    }
}
